package com.cyzy.controller;


import com.cyzy.dto.OrderDriverDto;
import com.cyzy.dto.OrderDto;
import com.cyzy.dto.ResponseDto;
import com.cyzy.pojo.TripOrder;
import com.cyzy.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;



//    用户打车后创建订单
    @PostMapping("/createOrder")
    public ResponseDto createOrder(@RequestBody TripOrder tripOrder){
//        System.out.println(tripOrder);
        if(tripOrder.getCarType()==null){
            return ResponseDto.fail("至少选择一种车型！");
        }
        long orderId =orderService.createOrder(tripOrder);
        if(orderId<=0){
            return ResponseDto.fail("生成订单失败！");
        }
        return ResponseDto.ok(String.valueOf(orderId));
    }
    //通过订单Id查订单
    @GetMapping("/orderInfo")
    public ResponseDto getInfo(@RequestParam("orderId") long orderId){
        TripOrder order = orderService.getById(orderId);
        if(order == null){
            return ResponseDto.fail("查询错误");
        }
        return ResponseDto.ok(order);
    }
    //判断订单是否被司机接单
    @GetMapping("isRob")
    public ResponseDto isRob(@RequestParam("orderId") long orderId){

        String rob = orderService.isRob(orderId);

        return ResponseDto.ok(rob);
    }

//    司机看到订单
    @GetMapping("/orderList")
    public ResponseDto orderList(@RequestParam("driverId") int driverId){
//        System.out.println(driverId);
        List<TripOrder> orderList=orderService.orderList(driverId);
        return ResponseDto.ok(orderList);
    }

    //长时间没司机接单删除订单
    @PostMapping("/deleteOrder")
    public ResponseDto deleteOrder(@RequestParam("orderId") long orderId){

        orderService.deleteOrder(orderId);
        return ResponseDto.ok("删除成功");
    }

//    司机接单-抢单
    @PostMapping("/orderTaking")
    public ResponseDto orderTaking(@RequestBody TripOrder tripOrder) {
//        System.out.println(tripOrder.getDriverId());
//        System.out.println(tripOrder.getOrderId());
//        System.out.println(tripOrder.getPrice());
        System.out.println(tripOrder + "   ---90-");
        ResponseDto reuslt=orderService.orderTaking(tripOrder);
        return reuslt;
    }


    //司机点击确认到达出发地
    @GetMapping("/confirmStart")
    public ResponseDto confirmStart(@RequestParam("driverId") Integer driverId){
        System.out.println(driverId);
        TripOrder order=orderService.confirmStart(driverId);
        return ResponseDto.ok(order);
    }

    //司机点击确认到达目的地
    @GetMapping("/confirmEnd")
    public ResponseDto confirmEnd(@RequestParam("driverId") Integer driverId){
        System.out.println(driverId);
        TripOrder order=orderService.confirmEnd(driverId);
        if(order==null){
            return ResponseDto.fail("确认到达目的地失败！");
        }
        return ResponseDto.ok(order);
    }

    //司机查看订单(全部订单)
    @GetMapping("/selectAllOrderList")
    public ResponseDto selectAllOrderList(@RequestParam("driverId") Integer driverId){
        System.out.println(driverId);
        List<OrderDto> orderDto=orderService.selectAllOrderList(driverId);
        return ResponseDto.ok(orderDto);
    }

    //通过订单id查看订单信息和司机信息
    @GetMapping("/selecetOrderAndDriver")
    public ResponseDto selecetOrderAndDriver(@RequestParam("orderId") long orederId){
        OrderDriverDto orderDriverDto = orderService.selecetOrderAndDriver(orederId);
        return ResponseDto.ok(orderDriverDto);
    }


    //司机查看系统派单的订单
    @GetMapping("/selectSystemOrderList")
    public ResponseDto selectSystemOrderList(@RequestParam("driverId") Integer driverId){
        System.out.println(driverId);
        List<OrderDto> orderDto=orderService.selectSystemOrderList(driverId);
        return ResponseDto.ok(orderDto);
    }

    //司机查看订单(进行中订单)
    @GetMapping("/selectOngoingOrderList")
    public ResponseDto selectOngoingOrderList(@RequestParam("driverId") Integer driverId){
        System.out.println(driverId);
        List<OrderDto> orderDto=orderService.selectOngoingOrderList(driverId);
        return ResponseDto.ok(orderDto);
    }

    //司机查看订单(已完成订单)
    @GetMapping("/selectCompletedOrderList")
    public ResponseDto selectCompletedOrderList(@RequestParam("driverId") Integer driverId){
        System.out.println(driverId);
        List<OrderDto> orderDto=orderService.selectCompletedOrderList(driverId);
        return ResponseDto.ok(orderDto);
    }

    //用户取消订单(只能是已接单并且司机还未载到乘客的订单可以取消)
    @PutMapping("/cancelOrder")
    public ResponseDto cancelOrder(@RequestBody TripOrder order){
        System.out.println(order);
        ResponseDto result = orderService.cancelOrder(order);
        return result;
    }

    //司机查看订单(司机接单的界面)
    @GetMapping("/selectWaitingOrderList")
    public ResponseDto selectWaitingOrderList(@RequestParam("driverId") Integer driverId){
        System.out.println(driverId);
        List<OrderDto> orderDto=orderService.selectWaitingOrderList(driverId);
        return ResponseDto.ok(orderDto);
    }

    //用户端查看订单（已完成，已支付）
    @GetMapping("/selectCompleteThePayment")
    public ResponseDto selectCompleteThePayment(@RequestParam("userId") Integer userId){
        System.out.println(userId);
        List<OrderDto> orderDtos = orderService.selectCompleteThePayment(userId);
        return ResponseDto.ok(orderDtos);
    }

    //用户端查询订单（已完成，未支付）
    @GetMapping("/selectCompletedUnpaid")
    public ResponseDto selectCompletedUnpaid(@RequestParam("userId") Integer userId){
        System.out.println(userId);
        List<OrderDto> orderDtos = orderService.selectCompletedUnpaid(userId);
        return ResponseDto.ok(orderDtos);
    }


    //用户查看订单(全部订单)
    @GetMapping("/selectPassengerAllOrderList")
    public ResponseDto selectPassengerAllOrderList(@RequestParam("userId") Integer userId){
        System.out.println(userId);
        List<OrderDto> orderDto=orderService.selectPassengerAllOrderList(userId);
        return ResponseDto.ok(orderDto);
    }

    //用户查看订单(待支付订单)
    @GetMapping("/selectPassengerPendingPaymentOrderList")
    public ResponseDto selectPassengerPendingPaymentOrderList(@RequestParam("userId") Integer userId){
//        System.out.println(userId);
        List<OrderDto> orderDto=orderService.selectPassengerPendingPaymentOrderList(userId);
        return ResponseDto.ok(orderDto);
    }

    //用户查看订单(取消的订单)
    @GetMapping("/selectPassengerCancelOrderList")
    public ResponseDto selectPassengerCancelOrderList(@RequestParam("userId") Integer userId){
        List<OrderDto> orderDto=orderService.selectPassengerCancelOrderList(userId);
        return ResponseDto.ok(orderDto);
    }

    //用户查看订单(已完成的订单)
    @GetMapping("/selectPassengerCompleteOrderList")
    public ResponseDto selectPassengerCompleteOrderList(@RequestParam("userId") Integer userId){
        List<OrderDto> orderDto=orderService.selectPassengerCompleteOrderList(userId);
        return ResponseDto.ok(orderDto);
    }

    //用户查看订单(待接单的订单)
    @GetMapping("/selectPassengerWaitingOrderList")
    public ResponseDto selectPassengerWaitingOrderList(@RequestParam("userId") Integer userId){
        List<OrderDto> orderDto=orderService.selectPassengerWaitingOrderList(userId);
        return ResponseDto.ok(orderDto);
    }

    //查询接单司机信息
    @GetMapping("/driverInformation")
    public ResponseDto driverInformation(@RequestParam("userId") long userId){
        //System.out.println(userId + " suerId");
        return orderService.driverInformation(userId);
    }

    //司机拒单(订单号、司机Id)
    @PutMapping("/rejectOrder")
    public ResponseDto rejectOrder(@RequestBody TripOrder order){
        System.out.println(order);
        ResponseDto result=orderService.rejectOrder(order);
        return result;
    }
}
